#include "text.h"


Bool printCharAt(char ch, int x, int y, char attributes){
	if(x < 0 || x > WIDTH || y < 0 || y > HEIGHT)
		return false;
	
	x = x-1;
	
	unsigned long offset = y*(WIDTH*2) + x*2;

	_farpokeb(_dos_ds, VIDEO_TEXT + offset, ch );
	_farpokeb(_dos_ds, VIDEO_TEXT + offset + 1, attributes);
	
	return true;
}

Bool printStringAt(char *str, int x, int y, char attributes){
	if(x < 0 || x > WIDTH || y < 0 || y > HEIGHT)
		return false;
	
	int i;
	
	unsigned long offset = y*(WIDTH*2) + x*2;
	ulong tammax = VIDEO_TEXT + HEIGHT*WIDTH*2;
	
	if((strlen(str) + offset) > tammax)
		return false;
	
	for(i = 0; i < strlen(str); i++){
		printCharAt(str[i], x+i, y, attributes);
	}
	return true;
}

Bool printIntAt(int num, int x, int y, char attributes){
	if(x < 0 || x > WIDTH || y < 0 || y > HEIGHT)
		return false;

	char *string = (char *) malloc(sizeof(char)*100);
	
	int i = 0;
	int numaux = num;
	
	while(numaux > 0){
		int ultnum = numaux%10;
		
		switch(ultnum){
			case 0: string[i] = '0'; break;
			case 1: string[i] = '1'; break;
			case 2: string[i] = '2'; break;
			case 3: string[i] = '3'; break;
			case 4: string[i] = '4'; break;
			case 5: string[i] = '5'; break;
			case 6: string[i] = '6'; break;
			case 7: string[i] = '7'; break;
			case 8: string[i] = '8'; break;
			case 9: string[i] = '9'; break;
			default: string[i] = '0'; 
		}
		numaux = numaux/10;
		i++;
	}
	string[i] = '\0';
	
	char stringdeint[i-1];
	int j = 0;
	
	while(i > 0){
		stringdeint[j] = string[i-1];
		j++;
		i--;
	}
	stringdeint[j] = '\0';
	
	printStringAt(stringdeint, x, y, attributes);
	
	return true;
}

Bool drawFrame(char *title, char attributes, int x, int y, int width, int height)
{
if(y < 0 || x < 0 || y+height >= HEIGHT || x+width >= WIDTH)
		return false;

char sup_esq = 201;
char horiz = 205;
char sup_dir = 187;
char vert = 186;
char inf_esq = 200;
char inf_dir = 188;
char fundo = ' ';
	 
	 
     //primeira linha (sem titulo)
     printCharAt(sup_esq,x,y,attributes);
     int i, t;
     for ( i = 1 ; i < width ; i++ )
         printCharAt(horiz,x+i,y,attributes);
     printCharAt(sup_dir,x+width,y,attributes);
 
     //corpo da frame
     for ( i = 1 ; i < height ; i++ )
     {
         printCharAt(vert,x,y+i,attributes);
         for ( t = 1 ; t < width ; t++ )
             printCharAt(fundo, x+t, y+i, attributes);
         printCharAt(vert,x+width,y+i,attributes);
     }
	 
    
     //ultima linha
     printCharAt(inf_esq,x,y+height,attributes);
     for ( i = 1 ; i < width ; i++ )
         printCharAt(horiz,x+i,y+height,attributes);
     printCharAt(inf_dir,x+width,y+height,attributes);
	 
  
     int posstring = (width/2) - (strlen(title)/2);
	printStringAt(title, x+posstring, y, attributes);

     return true;
}

void clear_text_screen(){
	int i, j;
	for(i = 0; i < WIDTH; i++){
		for(j = 0; j < HEIGHT; j++){
			printCharAt('o', i, j, 0);
		}
	}
}
